<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;

class Posts extends Model
{
    use HasFactory;
    use SoftDeletes;

    static $unguarded = true;

    protected $table = 'posts';

    /**
     * 根据ID获取数据
     *
     * @param $id
     * @param mixed $columns
     * @return array|Builder
     */
    public static function getDataByID($id, string $columns = ''){
        $data = self::query();
        if($columns){
            $data->find($id,$columns);
            return $data;
        }
        $val = $data->find($id);
        $status_show =  getStatusValue($val['status']);
        $image = $val['image'];
        if($image){
            $image_path =getImageUrl($image);
            $image_show = '<img width="60" height="60" src="'.$image_path.'">';
        }
        $username = '后台发布';
        if($val['user_id'] != null){
            $username = $val['username'];
        }
        $wheres = array();
        $wheres['post_id'] = $id;
        $wheres['type'] = 2;        // 1- 文字评论 2-图片
        $picCount =    PostsComment::countComment($wheres);

        $wheres = array();
        $wheres['post_id'] = $id;
        $comment_counts =    PostsComment::countComment($wheres);

         $orgInfo =    Organization::getDataByID($val['organizations_id']);

        $wheres = array();
        $wheres['post_id'] = $id;
        $likeCount =   PostsLikes::countLike($wheres);

        return [
            'id'=>$val['id'],
            'organizations_id'=>$val['organizations_id'],
            'pic_counts'=>$picCount,
            'image'=>$image,
            'image_path'=>$image_path??"",
            'image_show'=>$image_show??"",
            'title'=>$val['title'],

            'like_counts'=>$likeCount,

            'org_name'=>$orgInfo['name'],
            'org_created_at'=>$orgInfo['crated_at'],
            'org_avatar'=>getImageUrl($orgInfo['avatar']),

            'comment_counts'=>$val['comment_counts'],
            'content'=>$val['content'],
            'status'=>$val['status'],
            'user_id'=>$val['user_id'],
            'username'=>$username,
           // 'image_show'=>$image_show??"未上传",
            'status_show'=>$status_show,
            'sort'=>$val['sort'],
//            'likes_counts'=>$val['likes_counts'],
//            'status_html_show'=>$status_html_show,
            'crated_at'=>Carbon::parse($val['created_at'])->toDateTimeString(),
            'created_at'=>Carbon::parse($val['created_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),

            'comments_num'=>$comment_counts,
        ];
    }

    /**
     * 帖子评论
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function comment(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(PostsComment::class,'post_id','id');
    }

    /**
     * 帖子点赞
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function likes(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(PostsLikes::class,'post_id','id');
    }

    /**
     * 通过条件查找列表
     * 
     */
    public static function getPostsListByWhere($where,$field='*'){
        if(empty($where)){
            return [];
        }
        $data = self::query()->where($where)->select($field)->get();


        if(!empty($data)){
            foreach($data as $key=>$val){
                $data[$key]['image'] =  getImageUrl($val['image']);
                $data[$key]['created_at'] = date("Y-m-d H:i:s",strtotime($val['created_at']));
                $data[$key]['created_ats'] = strtotime($val['created_at']);
            }
        }


        return $data;
    }



    public static function getDataOne($id, string $columns = ''){
        $data = self::query();
        if($columns){
            $data->find($id,$columns);
            return $data;
        }
        $val = $data->find($id);
        $status_show =  getStatusValue($val['status']);
        $image = $val['image'];
        if($image){
            $image_path =getImageUrl($image);
            $image_show = '<img width="60" height="60" src="'.$image_path.'">';
        }
        $username = '后台发布';
        if($val['user_id'] != null){
            $username = $val['username'];
        }

        return [
            'id'=>$val['id'],
            'image'=>$image,
            'image_path'=>$image_path??"",
            'title'=>$val['title'],

            'content'=>$val['content'],
            'status'=>$val['status'],
            'user_id'=>$val['user_id'],
            'username'=>$username,
            // 'image_show'=>$image_show??"未上传",
            'status_show'=>$status_show,
            'sort'=>$val['sort'],
//            'likes_counts'=>$val['likes_counts'],
//            'status_html_show'=>$status_html_show,
            'crated_at'=>Carbon::parse($val['created_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),
        ];
    }

}
